import dateFormat from 'dateformat';

const filters = {
  // 毫秒转换 年月日-时分秒
  getTime(time) {
    if (time) {
      const date = new Date(time);
      const Y = `${date.getFullYear()}-`;
      const M = `${date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}-`;
      const D = `${date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()} `;
      const h = `${date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()}:`;
      const m = `${date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()}:`;
      const s = (date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds());
      return Y + M + D + h + m + s;
    }
    return '';
  },
  // 秒转换 年月日-时分秒
  getSTime(time) {
    if (time) {
      const date = new Date(time);
      const Y = `${date.getFullYear()}年`;
      const M = `${date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}月`;
      const D = `${date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()}日 `;
      const h = `${date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()}:`;
      const m = `${date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()}:`;
      const s = (date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds());
      return Y + M + D + h + m + s;
    }
    return '';
  },
  // 秒转换 时分秒
  getTimes(time) {
    if (time) {
      const day = Math.floor(time / (24 * 3600)); // Math.floor()向下取整
      const hour = Math.floor((time - day * 24 * 3600) / 3600);
      const minute = Math.floor((time - day * 24 * 3600 - hour * 3600) / 60);
      const second = time - day * 24 * 3600 - hour * 3600 - minute * 60;
      const h = hour > 10 ? hour : `0${hour}`;
      const m = minute > 10 ? minute : `0${minute}`;
      const s = second > 10 ? second : `0${second}`;
      return `${h}:${m}:${s}`;
    }
    return '00:00:00';
  },
  sizeFormat(value) {
    return `${parseInt(value)} KB`;
  },

  dateFormat(value) {
    return dateFormat(value, 'yyyy-mm-dd HH:MM:ss');
  },

  // 过滤文件名
  filterName(val) {
    if (val) {
      return val.split('/').reverse()[0] == '' ? val.split('/').reverse()[1] : val.split('/').reverse()[0];
    }
  },
};

export default function install(Vue) {
  Object.keys(filters).forEach((key) => {
    Vue.filter(key, filters[key]);
  });
}
